import request from '@/util/request.ts'

// 定义权限接口类型
export interface Permission {
  id?: number
  name: string
  code: string
  description: string
  createTime?: string
  updateTime?: string
}

export interface PermissionResponseData {
  code: number
  data: {
    records: Permission[]
    total: number
  }
  msg: string
  total: number
}

export interface PermissionListResponseData {
  code: number
  data: Permission[]
  msg: string
  total: number
}

export interface PermissionDetailResponseData {
  code: number
  data: Permission
  msg: string
  total: number
}

// 枚举API地址
enum API {
  // 分页查询权限
  PERMISSION_PAGE = '/admin/permission/page',
  // 查询所有权限
  PERMISSION_LIST = '/admin/permission/list',
  // 根据权限ID查询权限
  PERMISSION_BY_ID = '/admin/permission/',
  // 根据角色ID查询权限
  PERMISSION_BY_ROLE_ID = '/admin/permission/role/',
  // 新增权限
  ADD_PERMISSION = '/admin/permission',
  // 修改权限
  UPDATE_PERMISSION = '/admin/permission',
  // 删除权限
  DELETE_PERMISSION = '/admin/permission/',
}

// 分页查询权限
export const reqPermissionPage = (
  page: number,
  pageSize: number,
  searchObj: any = {},
) => {
  return request.get<any, PermissionResponseData>(
    `${API.PERMISSION_PAGE}?page=${page}&pageSize=${pageSize}`,
    { params: searchObj },
  )
}

// 查询所有权限
export const reqPermissionList = () => {
  return request.get<any, PermissionListResponseData>(API.PERMISSION_LIST)
}

// 根据权限ID查询权限
export const reqPermissionById = (id: number) => {
  return request.get<any, PermissionDetailResponseData>(
    `${API.PERMISSION_BY_ID}${id}`,
  )
}

// 根据角色ID查询权限
export const reqPermissionByRoleId = (roleId: number) => {
  return request.get<any, PermissionListResponseData>(
    `${API.PERMISSION_BY_ROLE_ID}${roleId}`,
  )
}

// 新增权限
export const reqAddPermission = (permission: Permission) => {
  return request.post<any, any>(API.ADD_PERMISSION, permission)
}

// 修改权限
export const reqUpdatePermission = (permission: Permission) => {
  return request.put<any, any>(API.UPDATE_PERMISSION, permission)
}

// 删除权限
export const reqDeletePermission = (id: number) => {
  return request.delete<any, any>(`${API.DELETE_PERMISSION}${id}`)
}
